using System;
using System.Collections.Generic;
using System.IO;

namespace Atomic.Vasp
{
	[Serializable]
	public class VaspIncar
	{
		private Dictionary<string, string> options;
		private Dictionary<string, string> comments;

		public VaspIncar()
		{
			options = new Dictionary<string, string>();
			comments = new Dictionary<string, string>();
		}

		public void Add(string name, string value)
		{
			Add(name, value, null);
		}

		public void Add(string name, string value, string comment)
		{
			if (options.ContainsKey(name) && options[name] != value)
			{
				throw new ArgumentException("Option already set to a different value.");
			}

			options.Add(name, value);

			if (comment != null)
			{
				comments.Add(name, comment);
			}
		}

		public void Write(DirectoryInfo directory)
		{
			using (StreamWriter writer = new StreamWriter(Path.Combine(directory.FullName, "INCAR")))
			{
				foreach (KeyValuePair<string, string> option in options)
				{
					string s = string.Format("{0} = {1}", option.Key, option.Value);
					if (comments.ContainsKey(option.Key))
					{
						s = s.PadRight(20) + " ! " + comments[option.Key];
					}
					writer.WriteLine(s);
				}
				writer.Close();
			}
		}
	}
}
